﻿@model Retete.ViewModels.ReteteViewModel
@{
    ViewBag.Title = "Retete";
}

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-1.4.4.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui.min.js")" type="text/javascript"></script>

<script type="text/javascript">
    window.onload = function () {
        if (FocusProdusNou.value == "True") ProdusNouNume.focus();
        else RetetaNouaCod.focus();
    }

    function submitOnEnter() {
        if (event.keyCode == "13") form.submit();
    } 
    function stergeInregistrare(id) {
        if (confirm('Doriti sa stergeti produsul selectat?') == true) {
            IDProdusSters.value = id;
            form.submit();
        }
    }
    function recalculareTotal() {
        RecalculareTotal.value = true;
        form.submit();
    }
</script>

<script type="text/javascript">
    $(function () {
        $("#ProdusNouNume").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '@Url.Content("~/Autocomplete/Produse")',
                    dataType: "json",
                    data: { maxRows: 8, contains: request.term },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { value: item.Cod + ' ' + item.Nume, nume: item.Nume, unitateMasura: item.UnitateMasura, id: item.ID, cod: item.Cod }
                        }));
                    }
                });
            },
            minLength: 2,
            delay: 50,
            select: function (event, ui) {
                ProdusNou_UnitateMasura.innerText = ui.item.unitateMasura;
                ProdusNou_Cod.innerText = ui.item.cod;
                ProdusNouID.value = ui.item.id;
                ProdusNouNume.value = ui.item.nume;
                ProdusNouCod.value = ui.item.cod;
            }
        });
    });
</script>

@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "form", name = "form" }))
{
    <table style="width: 100%">
        <tr>
            <td style="width: 190px; vertical-align: top; border-right-style: dashed; border-right-color: #CFCFCF; border-right-width: 1px;">
                <fieldset>
                    <legend>Activitati</legend>

                    <p class="toolbox"><a href="javascript:form.submit()">Accepta</a></p>
                    <p class="toolbox">@Html.ActionLink("Renunta", "Index")</p>
                </fieldset>
            </td>
            <td style="vertical-align: top;">
                @Html.ValidationSummary(false)
                @Html.HiddenFor(model => model.IDProdusSters)
                @Html.HiddenFor(model => model.FocusProdusNou)
                @Html.HiddenFor(model => model.RecalculareTotal)

                <fieldset>
                    <legend>Detalii reteta</legend>

                    <p>
                        @Html.LabelFor(model => model.RetetaNouaCod, "Cod")
                        @Html.TextBoxFor(model => model.RetetaNouaCod, new { placeholder = Model.CodUltimaReteta })
                    </p>

                    <p>
                        @Html.LabelFor(model => model.RetetaNouaNume, "Denumire")
                        @Html.TextBoxFor(model => model.RetetaNouaNume)
                    </p>

                    <p>
                        @Html.LabelFor(model => model.RetetaNouaCategorie, "Categorie")
                        @Html.TextBoxFor(model => model.RetetaNouaCategorie)
                    </p>

                    <p>
                        @Html.LabelFor(model => model.RetetaNouaMultiplicator, "Multiplicator")
                        @Html.TextBoxFor(model => model.RetetaNouaMultiplicator)
                    </p>

                    <p>
                        @Html.LabelFor(model => model.RetetaNouaPret, "Pret si cantitate")
                        @Html.TextBoxFor(model => model.RetetaNouaPret, new { style = "width: 145px;" })
                        @Html.TextBoxFor(model => model.RetetaNouaCantitate, new { style = "width: 146px" })
                    </p>

                    <p>
                        @Html.LabelFor(model => model.AdaosComercial, "Adaos si pret ambalaj")
                        @Html.TextBoxFor(model => model.AdaosComercial, new { style = "width: 145px;" })
                        @Html.TextBoxFor(model => model.PretAmbalaj, new { style = "width: 146px;" })
                    </p>
                </fieldset>
    
                <fieldset>
                    <legend>Produse participante</legend>
            
                    <table class="grid">
                        <thead>
                            <tr>
                                <th style="width: 16px"></th>
                                <th style="width: 70px">Cod</th>
                                <th>Produs</th>
                                <th style="width: 50px">Cant.</th>
                                <th style="width: 50px">UM</th>
                                <td style="width: 50px">Pret</td>
                                <td style="width: 50px">Valoare</td>
                                <td style="width: 50px">Portie</td>
                            </tr>
                        </thead>
        
                        <tbody>
                            <tr class="insertionRow">
                                <td></td>
                                <td id="ProdusNou_Cod"></td>
                                <td>
                                    @Html.TextBoxFor(model => model.ProdusNouNume, new { autocomplete = "off" })
                                    @Html.HiddenFor(model => model.ProdusNouID)
                                </td>
                                <td>@Html.TextBoxFor(model => model.ProdusNouCantitate, new { autocomplete = "off", onkeydown = "return submitOnEnter()" })</td>
                                <td id="ProdusNou_UnitateMasura"></td>
                                <td></td>
                                <td></td>
                                <td></td>
                            </tr>

                            @if (Model.Produse != null)
                            {
                                for (int i = 0; i < Model.Produse.Count; i++)
                                {
                                    <tr>
                                        <td>
                                            <img src="@Url.Content("~/Content/Images/Delete.png")" alt="Sterge" style="height: 12px; cursor: pointer;" onclick="stergeInregistrare(@Model.Produse[i].ID)" />
                                            @Html.HiddenFor(element => element.Produse[i].ID)
                                        </td>
                                        <td>
                                            @Html.DisplayTextFor(element => element.Produse[i].Produs.Cod)
                                            @Html.HiddenFor(element => element.Produse[i].Produs.Cod)
                                        </td>
                                        <td>
                                            @Html.DisplayTextFor(element => element.Produse[i].Produs.Nume)
                                            @Html.HiddenFor(element => element.Produse[i].Produs.Nume)
                                            @Html.HiddenFor(element => element.Produse[i].IDProdus)
                                        </td>
                                        <td class="insertionCell">@Html.TextBoxFor(element => element.Produse[i].Cantitate)</td>
                                        <td>
                                            @Html.DisplayTextFor(element => element.Produse[i].Produs.UnitateMasura)
                                            @Html.HiddenFor(element => element.Produse[i].Produs.UnitateMasura)
                                        </td>
                                        <td>
                                            @Model.Produse[i].Produs.PretCumparare.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)
                                            @Html.HiddenFor(element => element.Produse[i].Produs.PretCumparare)
                                        </td>
                                        <td>
                                            @Model.Produse[i].ValoareCumparare.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)
                                            @Html.HiddenFor(element => element.Produse[i].ValoareCumparare)
                                            @Html.HiddenFor(element => element.Produse[i].Reteta.MultiplicatorCantitati)
                                        </td>
                                        <td>
                                            @Model.Produse[i].CostCumpararePortie.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)
                                            @Html.HiddenFor(element => element.Produse[i].CostCumpararePortie)
                                        </td>
                                    </tr>
                                }
                                
                                <tr class="totals">
                                    <td></td>
                                    <td></td>
                                    <td><a href="javascript:recalculareTotal()">Total:</a></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td>@Model.TotalValoareCumparare.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)</td>
                                    <td>@Model.TotalCostCumpararePortie.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)</td>
                                </tr>
                                <tr class="totals">
                                    <td></td>
                                    <td></td>
                                    <td><a href="javascript:recalculareTotal()">Total cu adaos comercial:</a></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td></td>
                                    <td>@Model.TotalCostCuAdaos.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture)</td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </fieldset>
            </td>
        </tr>
    </table>
}